package com.codeforces;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

public class _116C {

	static Scanner scanner = new Scanner(System.in);
	static int N, maxn;
	static Vector<Integer>[] vects;
	static int[] num;

	public static void main(String[] args) {
		N = scanner.nextInt();
		vects = new Vector[N+1];
		for (int i=1;i<=N;++i) {
			vects[i] = new Vector<Integer>();
		}
		num = new int[N+1];
		for (int i=1;i<=N;++i) {
			int parent = scanner.nextInt();
			if (parent != -1) {
				vects[parent].add(i);
			}
			num[i] = parent;
		}
		maxn = 1;
		for (int i=1;i<=N;++i) {
			if (num[i] == -1) {
				dfs(i, 1);
			}
		}
		System.out.println(maxn);
	}

	private static void dfs(int node, int lev) {
		num[node] = lev;
		if (num[node] > maxn) {
			maxn = lev;
		}
		Iterator<Integer> it = vects[node].iterator();
		while (it.hasNext()) {
			int next = it.next();
			dfs(next, lev+1);
		}
	}

}
